<%@ page language="java"  pageEncoding="utf-8"%>
<%@taglib uri="/WEB-INF/struts-tags.tld" prefix="s"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>通讯录管理</title>
<%@include file="../common/jscss.jsp" %>
<style type="">
.com_txt{
	width: 180px;
}
</style>
<script type="text/javascript"><!--
	var editRoleId = "";
	$(function(){
		$.defUnit.treeMenu({
			callback:function(obj){
				var roleId = $(obj).attr("roleId");
				editRoleId = roleId;
				searchUser();
			}
		});
			
		$(".move_out_user").click(function(){
			if(confirm("你确定要移除用户吗？")){
				var memberId = $(this).attr("memberId");
				$.post("member!moveOutMember.do",{
					"id":memberId,
					"roleId":editRoleId
				},function(data){
					var ret = $.parseJSON(data);
					if(ret.success == 'true'){
						alert(ret.msg);
						searchUser();
					}else{
						alert(ret.msg);
					}
				})
			}
		});	
			
		$("#create_group_link").click(function(){
			$.defUnit.createGroup({
				callback:function(){
					var txt_group_name = $("#txt_group_name").val();
					$.post("member!createRole.do",{
						"role.name":txt_group_name,
						"role.parentId":editRoleId
					},function(data){
						var ret = $.parseJSON(data);
						alert(ret.msg);
					});
				}	
			});	
		});
		
		$("#delete_group_link").click(function(){
				var oTree=$("#contact_tree");
				var selectGroup=oTree.find("img.tree_arrow"),selectParent,selectGroupName;
				if(selectGroup.length<1){
					alert("请先选择分组，再进行删除！");	
				}else{
					selectGroupName=selectGroup.next("i").text();
					var oBtn=$("#btn_delete_group");
					var oGroupName=$("#d_group_name");
					oGroupName.text(selectGroupName);
					$.defUnit.popDiv({
						tcID:"delete_group"	
					});
					oBtn.unbind("click");
					oBtn.bind("click",function(){
					
						var child = $("#contact_tree img.tree_arrow").next().next();
						if(child.size() > 0){
							alert("该分组下还有子分组不能删除");
							return ;
						}else{
							selectGroup=oTree.find("img.tree_arrow");
							selectParent=selectGroup.parent();
							$.post("member!deleteRole.do",{
								"roleId":selectParent.attr("roleId")
							},function(data){
								var ret = $.parseJSON(data);
								if(ret.success == 'true'){
									$("a.tc_close").click();
									selectParent.remove();
									alert(ret.msg);
								}else{
									alert(ret.msg);
								}
							})
							
						}
					});	
						
				}
				/*
			$.defUnit.deleteGroup({
				callback:function(){
					
				}	
			});	
			*/
		});
		
		$("#rename_group_link").click(function(){
			$.defUnit.renameGroup({
				callback:function(){
					var new_name = $("#new_group_name").val();
					$.post("member!renameRole.do",{
						"role.name":new_name,
						"role.id":editRoleId
					},function(data){
						var ret = $.parseJSON(data);
						alert(ret.msg);
					});
				}	
			});	
		});
		
		var isBindAdd=true;
		$("#c_user_link").click(function(){
			$.defUnit.popDiv({
				tcID:"create_user", 
				zzBox:"zz_box",
				isZZ:true	
			});	
			
			if(isBindAdd){
				$.defUnit.treeMenu({
					treeID:"com_contact_tree"	
				});
				isBindAdd=false;
			}
			
			$.defUnit.downTreeFunc();
		});
		
		var oUserLi=$("#user_det").find("li");
		oUserLi.bind("click",function(){
			if($(this).hasClass("current")){
				$(this).removeClass("current");	
			}else{
				$(this).addClass("current");	
			}	
		});
		
		$("#user_select_all").bind("click",function(){
			oUserLi=$("#user_det").find("li");
			oUserLi.addClass("current");	
		});
		
		$("a.del_user").bind("click",function(event){
			var memberId = $(this).attr("memberId");
			if(confirm("你确定要删除吗？")){
				$.post("member!deleteUser.do",{
					"id":memberId
				},function(data){
					var ret = $.parseJSON(data);
					if(ret.success=='true'){
						searchUser();
					}else{
						alert(ret.msg);
					}
				})
			}
			event.stopPropagation();	
		});
		$("a.psd_update").bind("click",function(event){
			$.defUnit.popDiv({
				tcID:"psd_update", 
				zzBox:"zz_box",
				isZZ:true	
			});
			$("#btn_psd").attr("memberId",$(this).attr("memberId"));
			event.stopPropagation();	
		});
		
		$("#btn_psd").click(function(){
			if(confirm("你确定要重设密码吗？")){
				$.post("member!resetPwd.do",{
					"member.id":$(this).attr("memberId")
				},function(data){
					var ret = $.parseJSON(data);
					alert(ret.msg);
					$("#psd_update").hide();
					$("div.zz_box").hide();
				});
			}
		});
		
		var isBind=true;
		$("#move_group_link").bind("click",function(){
			if(isBind){
				$.defUnit.treeMenu({
					treeID:"move_contact_tree"	
				});
				isBind=false;
			}
			
			$.defUnit.moveGroup({
				callback:function(){
					var oUser=$("#user_det").find("li.current");
					if(oUser.length<1){
						alert("没有选择任何数据！");	
					}else{
						$.each(oUser,function(index,obj){
							var roleId = $("#m_new_group_name").attr("roleId");
							$.post("member!moveMember.do",{
								"member.id" : $(obj).find("span.c_name").attr("memberId"),
								"member.roleId" : roleId,
								"oldRoleId":editRoleId
							},function(data){
								var ret = $.parseJSON(data);
								if(ret.success != 'true'){
									alert(ret.msg);
								}
							});
						});	
						$("#m_contact").hide();
						$("div.zz_box").hide();
						alert("移动完毕");
						searchUser();
					}
				}	
			});	
		});
	});
--></script>
</head>

<body>
	<!-- head -->
	<%@include file="../common/header.jsp" %>
    <!-- head--end -->
    <div class="content">
    	<div class="p_pd_s1">
            <div class="box_s4 box_s5">
                <div class="title"><img src="images/other/icon_16.jpg" /><div class="manage_sorts"><a href="member!editRole.do" style="color: gray;">权限管理</a>&nbsp;&nbsp;&nbsp;<a href="member!userList.do">通讯录管理</a></div></div>
                <div class="p_content clearfix">
					<div class=" connect_box">
                    	<div class="ope_box">
                        	<div class="ope_det"><a href="javascript:void(0);" id="create_group_link">创建</a><a href="javascript:void(0);" id="rename_group_link">重命名</a><a href="javascript:void(0);" id="delete_group_link">删除</a></div>
                        	通讯录
                        </div>
                        <!-- tree_box -->
                        <div class="tree_box">
                            <ul class="tree_menu" id="contact_tree">
                                <s:iterator value="roleTree" id="roleTree">
                                	
		                    		<li class="t_s1 spread pl10" roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>">
		                            	<i><s:property value="name"/></i>
		                            	<s:if test="#roleTree.roleList != null && #roleTree.roleList.size() > 0">
		                            		<ul style="display:none;">
		                            			<s:iterator value="#roleTree.roleList" id="roleTree2">
		                            				<li class="t_s2 spread pl10 " roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>">
				                                    	<i><s:property value="name"/></i>
				                                    	<s:if test="#roleTree2.roleList != null && #roleTree2.roleList.size() > 0">
				                                        <ul class="tree_child" style="display:none;">
				                                        	<s:iterator value="#roleTree2.roleList" id="roleTree3">
				                                        	<li roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>"><i><s:property value="name"/></i></li>
				                                            </s:iterator>
				                                        </ul>
				                                        </s:if>
				                                    </li>
		                            			</s:iterator>
			                                	
			                                </ul>
		                            	</s:if>
		                                
		                             </li>
		                            
		                    	</s:iterator>
		                    	 <li class="t_s1 spread pl10 " roleId="0">
                                  	<i>未分组用户</i>
                                  </li>
                            </ul>
                        </div>
                        <!-- tree_box--end-->
                        
                    </div>
                    
                    <!-- detail_c_info -->
                    <div class="det_c_box" id="user_list">
                    	<div class="search_c_box">
                        	<a href="javascript:void(0)" class="def_btn_s2 fr mr30" id="c_user_link">创建用户</a>
                        	<div class="search_c">
                        		
                            	<input type="text" class="txt" id="search_key" value="<s:property value="search"/>"/><input type="button" class="btn" value=" " onclick="searchUser();"/>
                            </div>
                        </div>
                        <div class="det_info_box">
                        	<ul class="c_det_info" id="user_det">
                        		<s:iterator id="message" value="pageModel.list" status="status">
                        			<li memberId="<s:property value="id"/>">
	                                	<div class=" c_det_box">
	                                        <div class="c_rel_info">
	                                            <p style="margin-bottom: 5px;"><span>ID:&lt;<s:property value="id"/>#&gt;</span>&nbsp;&nbsp;&nbsp;<span class="c_name" memberId="<s:property value="id"/>"><s:property value="username"/></span><span><s:property value="schoolName"/></span></p>
	                                            <p style="margin-bottom: 5px;"><span class="c_phone" style="margin-right:40px;">手机：<s:property value="mobile"/></span><span class="c_mail">电子邮件：<s:property value="email"/></span></p>
	                                            <p><s:property value="pathName"/></p>
	                                        </div>
	                                        <div class="c_rel_ope">
	                                            <a href="javascript:void(0);" class=" mr15 psd_update" memberId="<s:property value="id" />">密码重设</a><a href="javascript:void(0);" class="mr15 move_out_user" memberId="<s:property value="id" />">移除用户</a><a href="javascript:void(0);" class="del_user" memberId="<s:property value="id" />">删除用户</a>
	                                        </div>
	                                    </div>
	                                </li>
                        		</s:iterator>
                            	
                               
                            </ul>
                            
                            <!-- fenye -->
                            <div class="c_ope_box">
                            	<div id="fy_container" class="contact_fy fr"></div>
       							<div class=" else_ope">
                                	<a href="javascript:void(0)" id="move_group_link">移动到</a><a href="javascript:void(0)" id="user_select_all">全选</a>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!-- fenye--end-->
                            
                        </div>
                    </div>
                    <!-- detail_c_info--end-->
                    
                </div>
            </div>
        </div>
    </div>
    <div style="clear: both;"></div>
    <%@include file="../common/footer.jsp" %>
    <div class="com_tc" id="create_group" style="display:none;">
    	<div class=" tc_title"><a href="javascript:void(0);" class="tc_close"><img src="images/other/icon_29.jpg" /></a>创建联系人分组</div>
        <div class="tc_content">

            <table class="tc_table">
                <tr>
                    <td width="40">组名</td><td><input type="text" class="com_txt" id="txt_group_name"  /></td>
                </tr>
                <tr>
                     <td width="40"></td><td><input type="submit" class="def_btn_s1" value="确 定" id="btn_group_name"  /></td>
                </tr>
            </table>

        </div>
    </div>
    
    <div class="com_tc" id="delete_group" style="display:none;">
    	<div class=" tc_title"><a href="javascript:void(0);" class="tc_close"><img src="images/other/icon_29.jpg" /></a>删除分组</div>
        <div class="tc_content">
        	<table class="tc_table">
            	<tr>
                	<td colspan="2">确定删除<span class="group_name ml10 mr10" id="d_group_name"></span>组</td>
                </tr>
                <tr>
                	 <td width="40"></td><td><input type="submit" class="def_btn_s1" value="确 定" id="btn_delete_group" /></td>
                </tr>
            </table>
        </div>
    </div>
    
    <div class="com_tc" id="rename_group" style="display:none;">
    	<div class=" tc_title"><a href="javascript:void(0);" class="tc_close"><img src="images/other/icon_29.jpg" /></a>重命名组名</div>
        <div class="tc_content">
        	<table class="tc_table">
            	<tr>
                	<td width="40">原组名</td><td><span class="group_name" id="old_group_name"></span></td>
                </tr>
                <tr>
                	<td width="40">新组名</td><td><input type="text" class="com_txt" id="new_group_name" /></td>
                </tr>
                <tr>
                	 <td width="40"></td><td><input type="submit" class="def_btn_s1" value="确 定" id="btn_rename_group" /></td>
                </tr>
            </table>
        </div>
    </div>
    
    <div class="com_tc" id="psd_update" style="display:none;">
    	<div class=" tc_title"><a href="javascript:void(0);" class="tc_close"><img src="images/other/icon_29.jpg" /></a>密码重设</div>
        <div class="tc_content">
        	<table class="tc_table">
            	<tr>
                	<td colspan="2">恢复初始密码：<span class="fb">111111</span></td>
                </tr>
                <tr>
                	 <td width="40"></td><td><input type="button" class="def_btn_s1" value="确 定" id="btn_psd" /></td>
                </tr>
            </table>
        </div>
    </div>
    
    <div class="com_tc com_tc_s1" id="create_user" style="display:none;">
    	<div class=" tc_title"><a href="javascript:void(0);" class="tc_close"><img src="images/other/icon_29.jpg" /></a>添加用户</div>
        <div class="tc_content">
			<div class=" com_tit_con fl">
            	<h3>添加用户</h3>
                <table class="tc_table">
                	 <tr>
                        <td width="80">分组</td>
                        <td>
                        	
                        	<div class="move_group">
                                <div class="show_new_group">
                                    <span class="select_group_name"></span>
                                    <a href="javascript:void(0);" class="down_arrow" id="com_down_arrow"><img src="images/other/icon_31.jpg" class="block" /></a>
                                    <div class="tree_box move_tree"  style="display:none;">
	                                    <ul class="tree_menu" id="com_contact_tree">
	                                         <s:iterator value="roleTree" id="roleTree">
	                                			<li class="t_s1 spread pl10" roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>">
					                            	<i><s:property value="name"/></i>
					                            	<s:if test="#roleTree.roleList != null && #roleTree.roleList.size() > 0">
					                            		<ul style="display:none;">
					                            			<s:iterator value="#roleTree.roleList" id="roleTree2">
					                            				<li class="t_s2 spread pl10 " roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>">
							                                    	<i><s:property value="name"/></i>
							                                    	<s:if test="#roleTree2.roleList != null && #roleTree2.roleList.size() > 0">
							                                        <ul class="tree_child" style="display:none;">
							                                        	<s:iterator value="#roleTree2.roleList" id="roleTree3">
							                                        	<li roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>"><i><s:property value="name"/></i></li>
							                                            </s:iterator>
							                                        </ul>
							                                        </s:if>
							                                    </li>
					                            			</s:iterator>
						                                	
						                                </ul>
					                            	</s:if>
					                                
					                             </li>
					                    	</s:iterator>
	                                    </ul>
	                                    <a href="javascript:void(0);" class="def_btn_s1 btn_move" id="com_btn_sure">确定</a> 
	                                </div>
                                    
                                    
                                </div>
                            </div>
                           
                        </td>
                    </tr>
                	<tr>
                        <td width="80">用户ID<font color="red">*</font></td><td><input type="text" class="com_txt" id="schoolcode"/></td>
                    </tr>
                    <tr>
                        <td width="80">姓名<font color="red">*</font></td><td><input type="text" class="com_txt" id="username"/></td>
                    </tr>
                    <tr>
                        <td width="80">单位<font color="red">*</font></td><td><input type="text" class="com_txt" id="schoolName"/></td>
                    </tr>
                    <tr>
                        <td width="80">手机<font color="red">*</font></td><td><input type="text" class="com_txt" id="mobile"/></td>
                    </tr>
                    <tr>
                        <td width="80">邮件</td><td><input type="text" class="com_txt" id="email"/></td>
                    </tr>
                   
                    <tr>
                         <td width="80"></td><td><input type="button" class="def_btn_s1" value="添 加" onclick="saveUser();"/></td>
                    </tr>
                </table>
            </div>
            
            <div class=" com_tit_con fl tc_pd_s1">
            	<h3>添加批量用户</h3>
                <p class="up_sort">从电脑上传</p>
                <div class=" upload_box">
                	<form action="member!userfile.do" enctype="multipart/form-data" id="userfileform">
                		<input type="hidden" name="roleId" id="upload_user_role_id" />
                 		<input type="file" name="userfile" class="file"/>
                 		<input type="button" onclick="uploadUser();" value="上传" style="width: 60px;height: 20px;left: 100px;position: absolute;z-index: 101;"/>
                 	</form>
                </div>
            </div>
            
            <div class="clear"></div>
            
        </div>
    </div>
    <div class="com_tc com_tc_s1" id="edit_user" style="display:none;width: 440px;">
    </div>
    <div class="com_tc" id="m_contact" style="display:none;">
    	<div class=" tc_title"><a href="javascript:void(0);" class="tc_close"><img src="images/other/icon_29.jpg" /></a>移动联系人到...</div>
        <div class="tc_content">
        	<table class="tc_table">
            	<tr>
                	<td width="40">原组</td><td><span class="group_name" id="m_old_group_name">优秀的老师</span></td>
                </tr>
                <tr>
                	<td width="40">新组</td>
                    <td>
                    	<div class="move_group">
                        	<div class="show_new_group">
                            	<span id="m_new_group_name"></span>
                                <a href="javascript:void(0);" class="down_arrow" id="down_arrow"><img src="images/other/icon_31.jpg" class="block" /></a>
                                <div class="tree_box move_tree" id="move_tree_wrap" style="display:none;">
                                    <ul class="tree_menu" id="move_contact_tree">
                                         <s:iterator value="roleTree" id="roleTree">
                                			<li class="t_s1 spread pl10" roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>">
				                            	<i><s:property value="name"/></i>
				                            	<s:if test="#roleTree.roleList != null && #roleTree.roleList.size() > 0">
				                            		<ul style="display:none;">
				                            			<s:iterator value="#roleTree.roleList" id="roleTree2">
				                            				<li class="t_s2 spread pl10 " roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>">
						                                    	<i><s:property value="name"/></i>
						                                    	<s:if test="#roleTree2.roleList != null && #roleTree2.roleList.size() > 0">
						                                        <ul class="tree_child" style="display:none;">
						                                        	<s:iterator value="#roleTree2.roleList" id="roleTree3">
						                                        	<li roleId="<s:property value="id"/>" title="组ID:<s:property value="id"/>"><i><s:property value="name"/></i></li>
						                                            </s:iterator>
						                                        </ul>
						                                        </s:if>
						                                    </li>
				                            			</s:iterator>
					                                	
					                                </ul>
				                            	</s:if>
				                                
				                             </li>
				                    	</s:iterator>
                                    </ul>
                                    <a href="javascript:void(0);" class="def_btn_s1 btn_move" id="btn_move_sure">确定</a> 
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                	 <td width="40"></td><td><input type="submit" class="def_btn_s1" value="移 动" id="m_users" /></td>
                </tr>
            </table>
        </div>
    </div>
    
    <script type="text/javascript">
    	$(function(){
			$.defUnit.pager({
				curPage:<s:property value="pageModel.currentPage" />,
				totalPage:<s:property value="pageModel.totalPage"/>,
				customStyle:"contact_fy",
				containerID:"fy_container",
				afterClickPage:function(page){
					var search_key = $("#search_key").val();
					$("#user_list").load("member!loadUser.do",{
						search:search_key,
						searchRole:editRoleId,
						page:page
					});
				}	
			});
			
			$("#user_det").find("li").dblclick(function(){
				var memberId = $(this).attr("memberId");
				$("#edit_user").load("member!editUser.do?id="+memberId,function(){
					$.defUnit.popDiv({
						tcID:"edit_user", 
						zzBox:"zz_box",
						isZZ:true	
					});
				});
				
			});
		});
		
		function saveUser(){
			
    		var schoolCode = $("#schoolcode").val();
    		var username = $("#username").val();
    		var roleid = editRoleId;
    		var mobile = $("#mobile").val();
    		var schoolName = $("#schoolName").val();
    		var email = $("#email").val();
    		if(schoolCode == null || schoolCode == ""){
    			alert("用户ID不能为空！");
    			return false;
    		}
    		if(username == null || username == ""){
    			alert("姓名不能为空！");
    			return false;
    		}
    		if(schoolName == null || schoolName == ""){
    			alert("单位不能为空！");
    			return false;
    		}
    		if(mobile == null || mobile == ""){
    			alert("手机不能为空！");
    			return false;
    		}
    		$.post("member!saveUser.do",{
    			'member.schoolCode':schoolCode,
    			'member.username':username,
    			'member.roleId':roleid,
    			'member.mobile':mobile,
    			'member.schoolName':schoolName,
    			'member.email':email,
    			'member.roleId':$("span.select_group_name").attr("roleId")
    		},function(data){
    			if(data!=null){
    				var ret = $.parseJSON(data);
    				if(ret.success == 'true'){
    					$("#schoolcode").val("");
    					$("#username").val("");
    					$("#mobile").val("");
    					$("#schoolName").val("");
    					$("#email").val("");
    					alert(ret.msg);
    				//	window.location.href="member!userList.do";
    				}else{
    					alert(ret.msg);
    				}
    			}
    		});
		}
		
		function searchUser(){
			var search_key = $("#search_key").val();
			$("#user_list").load("member!loadUser.do",{
				search:search_key,
				searchRole:editRoleId
			});
			
		}
    </script>
</body>
</html>
